package kt.com.bilin.coin.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoucl on 2017/8/15 0015.
 */

public class ActivityManger implements Application.ActivityLifecycleCallbacks {
    private static List<Activity> store;

    public static void registerActivityLifecycleCallbacks(Application application) {
        application.registerActivityLifecycleCallbacks(new ActivityManger());
        store = new ArrayList<>();
    }

    /**
     * 获取当前的Activity
     */
    public static Activity getCurActivity() {
        if (store.size() < 1) {
            return null;
        }
        return store.get(store.size() - 1);
    }

    /**
     * 判断栈中是否有该Activity
     */
    public static boolean isContainsActivity(String activityName) {
        for (Activity activity : store) {
            if (activity.getClass().getSimpleName().equals(activityName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取上一个Activity
     *
     * @return
     */
    public static Activity getpenultActivity() {
        return getActivity(store.size() - 2);
    }

    public static Activity getLastActivity() {
        if (store.size() < 1) {
            return null;
        }
        return store.get(store.size() - 2);
    }

    public static boolean isExistence(Class<?> clz) {
        Activity activity;
        for (int i = store.size() - 1; i >= 0; i--) {
            activity = store.get(i);
            if (activity != null && activity.getClass().isAssignableFrom(clz)) {
                return true;
            }
        }
        return false;
    }

    public static Activity getActivity(Class<?> clz) {
        if (store.size() < 1) {
            return null;
        }
        Activity activity;
        for (int i = store.size() - 1; i >= 0; i--) {
            activity = store.get(i);
            if (activity != null && activity.getClass().isAssignableFrom(clz)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 获取指定位置的Activity
     */
    public static Activity getActivity(int index) {
        if (store.size() < 1) {
            return null;
        }
        try {
            return store.get(index);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 关闭上一个页面
     */
    public static void finishUpActivity() {
        finishRegionActivity(1);
    }

    public static void finishActivity(Class<?> clz) {
        if (store.size() > 0) {
            Activity activity;
            for (int i = store.size() - 1; i >= 0; i--) {
                activity = store.get(i);
                if (activity != null && activity.getClass().isAssignableFrom(clz)) {
                    activity.finish();
                    break;
                }
            }
        }
    }

    /**
     * 关闭指定个数的上级页面
     *
     * @param number 需要关闭的上级数量
     */
    public static void finishRegionActivity(int number) {
        Activity activity;
        for (int i = 0; i < number; i++) {
            activity = getActivity(getSize() - 2 - i);
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public static int getSize() {
        return store.size();
    }

    public static void finishAllActivity() {
        try {
            Activity activity;
            for (int i = store.size() - 1; i >= 0; i--) {
                activity = store.get(i);
                if (activity != null) {
                    activity.finish();
                }
                store.remove(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void finishAllActivity(List<Class<?>> excludes) {
        Activity activity;
        int size = store.size();
        for (int i = size - 1; i >= 0; i--) {
            activity = store.get(i);
            try {
                boolean isExclude = false;
                for (Class<?> exclude : excludes) {
                    isExclude = activity.getClass().isAssignableFrom(exclude);
                    if (isExclude) break;
                }
                if (activity != null && !isExclude) {
                    activity.finish();
                }
            } catch (Exception e) {
                Logger.e(e);
            }
        }
    }

    public static void finishAllActivity(Class<?> exclude) {
        Activity activity;
        int size = store.size();
        for (int i = size - 1; i >= 0; i--) {
            activity = store.get(i);
            try {
                if (activity != null && !activity.getClass().isAssignableFrom(exclude)) {
//                    if (!activity.getClass().isAssignableFrom(MainActivity.class))
                    activity.finish();
                }
            } catch (Exception e) {
                Logger.e(e);
            }
        }
    }

    public static void exit() {
        try {
            finishAllActivity();
            System.gc();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            MobclickAgent.onProfileSignOff();
//            SophixManager.getInstance().killProcessSafely();
//            MobclickAgent.onKillProcess(App.getApp());
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        store.add(activity);
        com.yanzhenjie.sofia.Utils.setStatusBarDarkFont(activity, true);
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        store.remove(activity);
    }
}
